using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The EmployeeDesignationTableDB class is responsible for interacting with the database to retrieve and store information
/// about EmployeeDesignationTable objects.
/// </summary>
public class EmployeeDesignationTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of EmployeeDesignationTable from the underlying datasource.
/// </summary>
/// <param name="employeeDesignationKey">The unique EmployeeDesignationKey of the EmployeeDesignationTable in the database.</param>
/// <returns>An EmployeeDesignationTable when the EmployeeDesignationKey was found in the database, or null otherwise.</returns>
public static EmployeeDesignationTable GetItem(int employeeDesignationKey)
{
EmployeeDesignationTable myEmployeeDesignationTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeDesignationTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@employeeDesignationKey", employeeDesignationKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myEmployeeDesignationTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myEmployeeDesignationTable;
}
}

/// <summary>
/// Returns a list with EmployeeDesignationTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeDesignationTable objects.</returns>
public static EmployeeDesignationTableList GetList()
{
EmployeeDesignationTableList tempList = new EmployeeDesignationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeDesignationTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with EmployeeDesignationTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeDesignationTable objects.</returns>
public static EmployeeDesignationTableList GetListByJobDesignationCode(int JobDesignationCode)
{
EmployeeDesignationTableList tempList = new EmployeeDesignationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeDesignationTableSelectListByJobDesignationCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@jobDesignationCode", JobDesignationCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with EmployeeDesignationTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeDesignationTable objects.</returns>
public static EmployeeDesignationTableList GetListByUserAccountKey(int UserAccountKey)
{
EmployeeDesignationTableList tempList = new EmployeeDesignationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeDesignationTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a EmployeeDesignationTable in the database.
/// </summary>
/// <param name="myEmployeeDesignationTable">The EmployeeDesignationTable instance to save.</param>
/// <returns>The new EmployeeDesignationKey if the EmployeeDesignationTable is new in the database or the existing EmployeeDesignationKey when an item was updated.</returns>
public static int Save(EmployeeDesignationTable myEmployeeDesignationTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeDesignationTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myEmployeeDesignationTable.EmployeeDesignationKey == -1)
{myCommand.Parameters.AddWithValue("@employeeDesignationKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@employeeDesignationKey", myEmployeeDesignationTable.EmployeeDesignationKey);
}
if (myEmployeeDesignationTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myEmployeeDesignationTable.UserAccountKey);
}
if (myEmployeeDesignationTable.JobDesignationCode == null){
myCommand.Parameters.AddWithValue("@jobDesignationCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@jobDesignationCode", myEmployeeDesignationTable.JobDesignationCode);
}
if (myEmployeeDesignationTable.JobDescriptionDetail == null){
myCommand.Parameters.AddWithValue("@jobDescriptionDetail", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@jobDescriptionDetail", myEmployeeDesignationTable.JobDescriptionDetail);
}
if (myEmployeeDesignationTable.StartDate == null){
myCommand.Parameters.AddWithValue("@startDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@startDate", myEmployeeDesignationTable.StartDate);
}
if (myEmployeeDesignationTable.EndDate == null){
myCommand.Parameters.AddWithValue("@endDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@endDate", myEmployeeDesignationTable.EndDate);
}
if (myEmployeeDesignationTable.Remarks == null){
myCommand.Parameters.AddWithValue("@remarks", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remarks", myEmployeeDesignationTable.Remarks);
}
if (myEmployeeDesignationTable.Active == null){
myCommand.Parameters.AddWithValue("@active", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@active", myEmployeeDesignationTable.Active);
}
if (myEmployeeDesignationTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myEmployeeDesignationTable.Status);
}
if (myEmployeeDesignationTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myEmployeeDesignationTable.CreatedDateTime);
}
if (myEmployeeDesignationTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myEmployeeDesignationTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a EmployeeDesignationTable from the database.
/// </summary>
/// <param name="employeeDesignationKey">The EmployeeDesignationKey of the EmployeeDesignationTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int employeeDesignationKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeDesignationTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@employeeDesignationKey", employeeDesignationKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

public static EmployeeDesignationTable GetActiveItem(int UserAccountKey)
{
    EmployeeDesignationTable myEmployeeDesignationTable = null;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocEmployeeDesignationTableSelectActiveSingleItem", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.Read())
                {
                    myEmployeeDesignationTable = FillDataRecord(myReader);
                }
                myReader.Close();
            }
            myConnection.Close();
        }
        return myEmployeeDesignationTable;
    }
}

#endregion

/// <summary>
/// Initializes a new instance of the EmployeeDesignationTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static EmployeeDesignationTable FillDataRecord(IDataRecord myDataRecord )
{
EmployeeDesignationTable myEmployeeDesignationTable = new EmployeeDesignationTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmployeeDesignationKey")))
{
myEmployeeDesignationTable.EmployeeDesignationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EmployeeDesignationKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myEmployeeDesignationTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("JobDesignationCode")))
{
myEmployeeDesignationTable.JobDesignationCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("JobDesignationCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("JobDescriptionDetail")))
{
myEmployeeDesignationTable.JobDescriptionDetail = myDataRecord.GetString(myDataRecord.GetOrdinal("JobDescriptionDetail"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartDate")))
{
myEmployeeDesignationTable.StartDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("StartDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndDate")))
{
myEmployeeDesignationTable.EndDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("EndDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remarks")))
{
myEmployeeDesignationTable.Remarks = myDataRecord.GetString(myDataRecord.GetOrdinal("Remarks"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Active")))
{
myEmployeeDesignationTable.Active = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Active"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myEmployeeDesignationTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myEmployeeDesignationTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myEmployeeDesignationTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myEmployeeDesignationTable;
}

     
}
